package file;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;

public class FileComparator {
	
	/**
	 * Compare each line of the fileA and fileB.<br>
	 * The fileRes contains the results of the comparaison.<br>
	 * Each line is tagged with a [+] or a [-] depending of the result of the comparison.<br>
	 * Then writes the result into fileRes.
	 * 
	 * @param fileA File to compare with fileB
	 * @param fileB File to compare with fileA
	 * @param fileRes File wich contains the result of the comparaison
	 */
	public static void compare(final String fileA, final String fileB, final String fileRes) {
		StringBuffer sb = new StringBuffer();
		
		int nbPassed  = 0;
		int nbFailed  = 0;
		int nbTotal   = 0;
		
		// Header
		sb.append(buildHeader(fileA, fileB, fileRes));
		
		try {
			BufferedReader readerA = new BufferedReader(new FileReader(fileA));
		    BufferedReader readerB = new BufferedReader(new FileReader(fileB));
			
		    DecimalFormat formater = new DecimalFormat("00");		    
		    
			String inputA = readerA.readLine();
			String inputB = readerB.readLine();
			
			while (inputA != null || inputB != null) {
				
				if (inputA == null) {
					inputA = "";
				}
				else if (inputB == null) {
					inputB = "";
				}
				
				sb.append(formater.format(nbTotal));
				
				if (inputA.equals(inputB)) {
					sb.append(" [+] ");
					sb.append(inputA);
					
					++nbPassed;
				}
				else {
					sb.append(" [-] ");
					sb.append(inputA + " | returned : " + inputB);
					
					++nbFailed;
				}
				
				sb.append(PersistenceFile.LINE_SEPARATOR);
				
				inputA = readerA.readLine();
				inputB = readerB.readLine();						
				++nbTotal;		
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		
		// Footer
		sb.append(buildFooter(nbPassed, nbFailed, nbTotal));
		
		PersistenceFile.stringToFile(fileRes, sb.toString());
	}
	
	/**
	 * Build the res file's header.<br>
	 * 
	 * @param fileA
	 * @param fileB
	 * @param fileRes
	 * @return
	 */
	private static String buildHeader(final String fileA, final String fileB, final String fileRes) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("### ").append(fileRes).append(" ###").append(PersistenceFile.LINE_SEPARATOR);
		
		sb.append("File A : ").append(fileA).append(PersistenceFile.LINE_SEPARATOR);
		sb.append("File B : ").append(fileB).append(PersistenceFile.LINE_SEPARATOR);
		
		sb.append(PersistenceFile.LINE_SEPARATOR);
		
		return sb.toString();
	}
	
	/**
	 * Build the res file's footer.<br>
	 * Contains the number of passed / failled.<br>
	 * 
	 * @param nbPassed the number of passed lines
	 * @param nbFailed the number of failed lines
	 * @return
	 */
	private static String buildFooter(int nbPassed, int nbFailed, int nbTotal) {
		StringBuffer sb = new StringBuffer();
		
		sb.append(PersistenceFile.LINE_SEPARATOR);
		
		sb.append("~~ ");
		
		if (nbFailed == 0) {
			sb.append(" 0-Defect ");
		}
		else {
			sb.append(" Defect ");
		}
		
		sb.append(" ~~");
		
		sb.append(PersistenceFile.LINE_SEPARATOR);
		sb.append(PersistenceFile.LINE_SEPARATOR);
		
		sb.append("Overview    : ").append(nbTotal).append(" cases").append(PersistenceFile.LINE_SEPARATOR);
		sb.append("[+] Passed  : ").append(nbPassed).append(PersistenceFile.LINE_SEPARATOR);
		sb.append("[-] Failed  : ").append(nbFailed).append(PersistenceFile.LINE_SEPARATOR);
		
		return sb.toString();
	}
	
	
	
}
